export default {
    getShader: function () {
        return `
        precision mediump float;
        uniform vec4 u_line_Color;
        uniform vec2 u_Angle;
        varying float v_Set;
        float pi = radians(180.0);
        float getCosJ(){
            vec2 c = vec2(gl_PointCoord);
            vec2 mo = c - vec2(0.5,0.5);
            float l = mo.x;
            float x = distance(c,vec2(0.5,0.5));
            float cosj = l / x;
            return cosj;
        }
    
        bool getAngle(float j1,float j2,float t){
            float cosj = getCosJ() * t;
            float a = 0.0;
            float b = 0.0;
            if(t > 0.0){
                if(j1 < j2){
                    a = cos(min(j1, 180.0) * pi / 180.0);
                    b = cos(min(j2, 180.0) * pi / 180.0);
                    if(a >= cosj && cosj >= b){
                        return true;
                    }
                }else{
                    //逆开角(先不算)
                    a = cos(min(j2, 180.0) * pi / 180.0);
                    b = cos(min(j1, 180.0) * pi / 180.0);
                    if(!(a >= cosj && cosj >= b)){
                        return true;
                    }
                }
            }else{
                //负角
                if(j1 < j2){
                    a = cos(max(j1 - 180.0, 0.0) * pi / 180.0);
                    b = cos(max(min(j2 - 180.0, 180.0), 0.0) * pi / 180.0);
                    if(a >= cosj && cosj >= b){
                        return true;
                    }
                }else{
                    //逆开角(先不算)
                    a = cos(max(j2 - 180.0, 0.0) * pi / 180.0);
                    b = cos(max(min(j1 - 180.0, 180.0), 0.0) * pi / 180.0);
                    if(!(a >= cosj && cosj >= b)){
                        return true;
                    }
                }
            }
    
            return false;
        }
    
        //获取弧线头部的圆心到圆心的距离
        // j:从[0.5,0.5]的角度 posiRadius:弧线半径 radius:画的圆的半径 diffRadius:扩散半径
        float getAngular(float j, float posiRadius){
            float x = posiRadius;
            float l = x * cos(j * pi / 180.0);
            float d = x * sin((360.0 - j) * pi / 180.0);
    
            vec2 c = vec2(l + 0.5, d + 0.5);
            float dist = distance(gl_PointCoord, c);
            return dist;
        }
    
        //画弧线
        bool drawAngle(
            float j1, float j2,
            float lineStart, float lineEnd,
            float lineDiff,
            out vec4 color
        ){
            bool s = false;
            if(gl_PointCoord.y <= 0.5){
                s = getAngle(j1, j2, 1.0);
            }else{
                s = getAngle(j1, j2, -1.0);
            }
            if(s == true){
                float dist = distance(gl_PointCoord, vec2(0.5,0.5));
                if(dist <= lineStart && dist >= lineEnd){
                    
                    if(dist >= lineStart - lineDiff){
                        //外渐变
                        color.a *= 1.0 - (dist - (lineStart - lineDiff)) / (lineDiff);
                        return false;
                    }else if(dist <= lineEnd + lineDiff){
                        //内渐变
                        color.a *= (dist - lineEnd) / (lineDiff);
                        return false;
                    }else{
                        //实线
                        return false;
                    }
                }
            }
            return true;
        }
    
        bool drawAngular(
            float j1, float j2,
            float lineStart, float lineRadius, float lineDiff,
            out vec4 color
        ){
            float dist = getAngular(j1, lineStart - lineRadius - lineDiff);
            if(dist < lineRadius + lineDiff){
                color.a *= 1.0 - (dist - lineRadius) / (lineDiff);
                return false;
            }
    
            dist = getAngular(j2, lineStart - lineRadius - lineDiff);
            if(dist < lineRadius + lineDiff){
                color.a *= 1.0 - (dist - lineRadius) / (lineDiff);
                return false;
            }
            return true;
        }
    
        float getJ2(float j1, float j2, float set){
            float offAngle = 0.5;
            float curJ2 = j2;
            if(set <= offAngle){
                if(j1 > curJ2){
                    curJ2 += 360.0;
                }
                curJ2 = (curJ2 - j1) * (set / offAngle) + j1;
                if(curJ2 > 360.0){
                    curJ2 -= 360.0;
                }
            }else{
                curJ2 = j2;
            }
            return curJ2;
        }
    
        float getJ1(float j1, float j2, float set){
            float offAngle = 0.5;
            if(set >= offAngle){
                float xnj2 = j2;
                if(xnj2 < j1){
                    xnj2 += 360.0;
                }
                j1 = (xnj2 - j1) * ((set - offAngle) / (1.0 - offAngle)) + j1;
    
                if(j1 > 360.0){
                    j1 -= 360.0;
                }
            }
            return j1;
        }
    
        uniform vec3 u_Line;
    
        void main(){
            float u_line_S = u_Line.x;
            float u_line_Dia = u_Line.y;//直径
            float u_line_Diff = u_Line.z;//扩散半径
            float u_line_E = u_line_S - u_line_Dia - u_line_Diff * 2.0;
            float set = v_Set;
            vec4 color = u_line_Color;

            if(set >= 1.0){
                return;
            }
    
            float j1 = u_Angle.x;
            //计算角二
            float j2 = getJ2(j1, u_Angle.y, set);
    
            j1 = getJ1(j1, j2, set);
    
            j1 += set * 100.0;
            j2 += set * 100.0;
            if(j1 >= 360.0){
                j1 -= 360.0;
            }
            if(j2 >= 360.0){
                j2 -= 360.0;
            }
                
            if(set > 0.7){
                color.a *= 1.0 - (set - 0.7) / 0.2;
            }
    
            //计算弧线
            if(drawAngle(j1, j2, u_line_S, u_line_E, u_line_Diff, color) == false){
                gl_FragColor = color;
                return;
            }
            //计算圆角
            if(drawAngular(j1, j2, u_line_S, u_line_Dia / 2.0, u_line_Diff, color) == false){
                gl_FragColor = color;
                return;
            }
           
        }
        `;
    }
}
